package com.Controller.Admin;

import com.Common.CodeMsg;
import com.Common.ResultModel;
import com.Model.Admin.CarInfo;
import com.Model.Admin.car_fee_mgr;
import com.Service.Admin.CarFeeMgrService;
import com.Service.Admin.CarInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

@Controller
public class CarMngrController {

    @Autowired
    CarFeeMgrService carFeeMgrService;

    @Autowired
    CarInfoService carInfoService;

    @RequestMapping(value = "/carmgr/index", method = RequestMethod.GET)
    public String index() {
/*
        List<car_fee_mgr> lst=carFeeMgrService.getCarFeeLst();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("lst",lst);*/
        return "admin/carFee";
    }

    @RequestMapping(value = "/carmgr/getlst", method = RequestMethod.GET)
    @ResponseBody
    public List<car_fee_mgr> getCarFeeLst() {
        List<car_fee_mgr> lst = carFeeMgrService.getCarFeeLst();
        return lst;
    }

    @RequestMapping(value = "/admin/CarMnger")
    public String carIndex() {
        return "admin/Carmanager";
    }

    @RequestMapping(value = "/admin/carInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel userInfo(CarInfo carInfo, BindingResult result) throws IOException {

        ResultModel resultModel;
        if (result.hasErrors()) {
            resultModel = new ResultModel(CodeMsg.param_is_empty.getIndex(), CodeMsg.param_is_empty.getName(), null);
            return resultModel;
        }

        PageInfo<CarInfo> lst = carInfoService.getCarInfoByQuery(carInfo);

        if (lst == null) {
            resultModel = new ResultModel(CodeMsg.nonData.getIndex(), CodeMsg.nonData.getName(), null);
        } else {
            resultModel = new ResultModel(CodeMsg.success.getIndex(), CodeMsg.success.getName(), lst.getList());
        }

        return resultModel;
    }

}
